
import { PrismaClient } from '@prisma/client'

const prisma = new PrismaClient()
import { NextResponse } from 'next/server';
import { PageParams } from '@/types';
import { LoveCardType } from '@/types/book';

export async function GET(request: Request) {
  const data = await request.json();
  const { limit = 10, offset = 0 }: PageParams = data;

  const books = await prisma.loveDoCard.findMany({
    take: limit,
    skip: offset
  })

  return NextResponse.json({ message: '获取成功', data: books }, { status: 200 });
}
// 名字
// name         String

export async function POST(request: Request) {
  const data = await request.json();
  const { id, name, }: LoveCardType = data;

  if (id !== undefined) {
    await prisma.loveDoCard.update({
      where: { id },
      data: { name, }
    })
  } else {
    // 获取最后一条数据
    const lastData = await prisma.loveDoCard.findFirst({
      orderBy: { id: 'desc' }
    })
    let newId = 0
    if (lastData) {
      newId = lastData.id + 1
    }
    await prisma.loveDoCard.create({
      data: { name, id: newId }
    })
  }

  // 返回成功响应
  return NextResponse.json({ message: '提交成功', data: 0 }, { status: 200 });
}

/**
 * 删除
 * @returns 
 */
export async function DELETE(request: Request) {
  const data = await request.json();
  const { id }: LoveCardType = data;

  await prisma.loveDoCard.delete({
    where: { id }
  })

  // 返回成功响应
  return NextResponse.json({ message: '删除成功', data: 0 }, { status: 200 });
}

/**
 * 批量增加
 */
const dataSource = [
   {
      name: "庆祝最近获得的小成就",
   }, {
      name: "把烦心事写下来再撕掉",
   }, {
      name: "打开新闻APP看看本周发生了什么事儿",
   }, {
      name: "找一片形状特别的树叶做书签",
   }, {
      name: "打一场羽毛球出一场汗",
   }, {
      name: "睡前喝杯牛奶不熬夜",
   }, {
      name: "练习冥想",
   }, {
      name: "相信本周内会有好事发生",
   }, {
      name: "感受凉风拂面的快乐",
   }, {
      name: "看看星星和月亮",
   }, {
      name: "泡个泡泡浴或洗个热水澡",
   }, {
      name: "捏捏脸，笑一个",
   }, {
      name: "去宠物店看看可爱的小动物",
   }, {
      name: "学游泳或者游一次泳",
   }, {
      name: "吃个冰凉凉的西瓜",
   }, {
      name: "看看天边的晚霞",
   }, {
      name: "点一杯想喝的饮料",
   }, {
      name: "试着早起聆听鸟鸣",
   }, {
      name: "体验一次蹦极",
   }, {
      name: "学唱一首新歌",
   }, {
      name: "做20分钟瑜伽",
   }, {
      name: "写下3个优点",
   }, {
      name: "重温一部最爱的电影",
   }, {
      name: "计划一次短途旅行",
   }, {
      name: "尝试做一些轻松的家务",
   }, {
      name: "在热闹的街道上",
   }, {
      name: "逛逛美术馆感受人群熙攘",
   }, {
      name: "去树荫下乘凉",
   }, {
      name: "玩一玩新游戏",
   }, {
      name: "去坐公交车沿路看风景",
   }, {
      name: "骑车感受在城市里穿梭的快乐",
   }, {
      name: "组织一次烧烤聚会",
   }, {
      name: "调制一杯特殊的饮品",
   }, {
      name: "买一个小饰品",
   }, {
      name: "买下收藏夹的第一个商品",
   }, {
      name: "逛逛文具店买些文具",
   }, {
      name: "买一个收纳箱",
   }, {
      name: "整理手机APP图标",
   }, {
      name: "整理收藏夹",
   }, {
      name: "扔掉很久不用的东西",
   }, {
      name: "定一个学习计划",
   }, {
      name: "计划一次海边旅行",
   }, {
      name: "制定一个爬山计划",
   }, {
      name: "看一部感兴趣的高分电视剧",
   }, {
      name: "买一份爱吃的零食吃掉它",
   }, {
      name: "看一部感人的高分电影",
   }, {
      name: "吃一份满意的午餐",
   }, {
      name: "改变房间格局换下心情",
   }, {
      name: "逛一逛小吃街吃点好吃的",
   }, {
      name: "买点爱吃的水果",
   }, {
      name: "拉好窗帘睡个午觉",
   }, {
      name: "为一张喜欢的图片点赞",
   }, {
      name: "做一件爱护环境的小事",
   }, {
      name: "慢跑",
   }, {
      name: "去天桥上看远方的日落",
   }, {
      name: "和喜欢的小动物进行互动",
   }, {
      name: "动手染发",
   }, {
      name: "和路边的小猫小狗 say hi~",
   }, {
      name: "在凉爽的傍晚进行一次慢跑",
   }, {
      name: "给冰箱进一次货",
   }, {
      name: "做一个手工",
   }, {
      name: "看一本想看的书",
   }, {
      name: "聆听下雨的声音",
   }, {
      name: "为天边的云彩拍张照",
   }, {
      name: "逛夜市",
   }, {
      name: "听一首上个世纪的歌曲",
   }, {
      name: "在沙发上来一个葛优瘫",
   }, {
      name: "发10分钟呆",
   }, {
      name: "转发一条锦鲤微博",
   }, {
      name: "对着镜子笑一笑",
   }, {
      name: "为热门视频点个赞",
   }, {
      name: "去KTV唱歌",
   }, {
      name: "打卡一家新店",
   }, {
      name: "去吃一次火锅",
   }, {
      name: "拼乐高或拼图",
   }, {
      name: "买一束花",
   }, {
      name: "换一个新发型",
   }, {
      name: "吃一块蓬松的奶油蛋糕",
   }, {
      name: "去图书馆看书",
   }, {
      name: "做一次饭",
   },
]
export async function PUT(request: Request) {
  // 删除所有数据
  await prisma.loveDoCard.deleteMany()
  // 准备数据
  const data = dataSource.map((item, index) => {
    return {
      name: item.name,
      id: index
    }
  })
  // 批量增加
  await prisma.loveDoCard.createMany({
    data,
    
  })

  return NextResponse.json({ message: '批量增加成功', data: 0 }, { status: 200 });
}